#include "CrazyTollgateOperateLayer.h"
#include "global/GlobalPath.h"
#include "global/GlobalDefine.h"
#include "global/GlobalParam.h"
#include "util/SceneManager/SceneManager.h"

CrazyTollgateOperateLayer::CrazyTollgateOperateLayer(){
	m_scoreLabel = NULL;
	m_score = 0;
}

CrazyTollgateOperateLayer::~CrazyTollgateOperateLayer(){
	NOTIFY->removeAllObservers(this);
}

bool CrazyTollgateOperateLayer::init(){
	bool bRet = false;
	do 
	{
		addWins();

		NOTIFY->addObserver(this,callfuncO_selector(CrazyTollgateOperateLayer::addScore),SCORE_ADD_MSG,NULL);

		NOTIFY->addObserver(this,callfuncO_selector(CrazyTollgateOperateLayer::gameover),GAMEOVER_MSG,NULL);

		bRet = true;
	} while (0);
	return bRet;
}

void CrazyTollgateOperateLayer::addWins(){
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	m_scoreLabel = CCLabelTTF::create("0",PATH_FONT_PUBLIC_Quartz_MS,GlobalParam::PublicFontSizeLarge);
	this->setPosition(ccp(m_scoreLabel->getContentSize().width,visibleSize.height - m_scoreLabel->getContentSize().height));
	this->addChild(m_scoreLabel);
}

void CrazyTollgateOperateLayer::addScore(CCObject* pData){
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	m_score += NUM_SCORE_ADD;
	CCString* scoreLayer = CCString::createWithFormat("%d",m_score);
	m_scoreLabel->setString(scoreLayer->getCString());
	this->setPosition(ccp(m_scoreLabel->getContentSize().width/2 + visibleSize.width/20
		,visibleSize.height - m_scoreLabel->getContentSize().height));
}

void CrazyTollgateOperateLayer::gameover(CCObject* pData){
	CCUserDefault::sharedUserDefault()->setIntegerForKey("crazyScore",m_score);
	SceneManager::shareSceneManager()->changeScene(enCrazyScoreScene);
}